<?php
chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";

include_once "includes/switcher.php";



class ShipmentList extends IPlugin 
{
    var $color;
    
    function ShipmentList($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->color = "blue";
    }
    
    function getPluginTag() { return "shipmentlist"; }
    function getPluginName() { return "Shipment List Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuBlockTitle", "MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Shopping cart"; } break;
            case "MenuTitle" : { return "Shipping"; } break;
        };
        return $result;
    }

    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------
    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        $arg = $core->getGET();
        
        if("echo" == $arg["action"])
        {
            echo "ShipmentList: echo: ".$arg["string"];
        }
        if("doDeleteShipment" == $arg["action"] && $arg["id"]>0)
        {
            $db = $core->getProperty("db");
            $db->Exec("delete from shipment where shipment_id={$arg[id]}");
        }

            #################
            #  UPS CP START #
            #################
            
        if('ups_tracking_change' == $_POST['event']){
            if(isset($_POST['ups_tracker'])){       
                $sw = new myswitch('ups_tracker',$db);
                $sw->set_key_value('1');
            }
            else{       
                $sw = new myswitch('ups_tracker',$db);
                $sw->set_key_value('0');
            }
        }
        
        if('ups_tit_change' == $_POST['event']){
            if(isset($_POST['ups_tit'])){       
                $sw = new myswitch('ups_tit',$db);
                $sw->set_key_value('1');
            }
            else{       
                $sw = new myswitch('ups_tit',$db);
                $sw->set_key_value('0');
            }
        }
        
        if('ups_av_change' == $_POST['event']){
            if(isset($_POST['ups_av'])){        
                $sw = new myswitch('ups_av',$db);
                $sw->set_key_value('1');
            }
            else{       
                $sw = new myswitch('ups_av',$db);
                $sw->set_key_value('0');
            }
        }
        
        if('ups_rate_change' == $_POST['event']){
            if(isset($_POST['ups_rate'])){      
                $sw = new myswitch('ups_rate',$db);
                $sw->set_key_value('1');
            }
            else{       
                $sw = new myswitch('ups_rate',$db);
                $sw->set_key_value('0');
            }
        }
            #################
            #  UPS CP END   #
            #################


        if("doSubmitShipment" == $_POST["action"])
        {
                        
            /* Edit EVENT */
            /* Edit EVENT */
            /* Edit EVENT */

            if($_POST['event'] == 'edit'){
            $z = $_POST;
            $uid = $z["id"];
            unset($z["action"], $z["id"],$z['event']);

            if (!in_array('active', array_keys($z))) {
                $z["active"] = 'N';
            }

            else{
               if ('on' == $z['active']) 
                $z["active"] = 'Y';
            }

            $sql = "";
            foreach ($z as $k => $v)
            {
                $sql .= ($sql=="" ? "" : ", ")."{$k} = '".str2db($v)."'";
            }
            
            
            $sql = "update shipment set {$sql} where shipment_id={$uid}";
            $db->Exec($sql);
            }

            /* ADD EVENT */
            /* ADD EVENT */
            /* ADD EVENT */

            if($_POST['event'] == 'add'){
            
            $z = $_POST;
            $uid = $z["id"];
            unset($z["action"], $z["id"],$z['event']);

            if (!in_array('active', array_keys($z))) {
                $z["active"] = 'N';
            }

            else{
               if ('on' == $z['active']) 
                $z["active"] = 'Y';
            }

            


            $sql = "";

            foreach ($z as $k => $v)
            {
                        $keys .=($keys=="" ? "" : ", ")."{$k}";
                        $vars .=($vars=="" ? "" : ", ")."'".str2db($v)."'";
            }
        
            $sql="insert into shipment ({$keys}) values({$vars}) ";

            $core = getCoreInstance();
            $db = $core->getProperty("db");
            $db->Exec($sql);            

            unset($_POST);          

            }
        }
        
        return true;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------
    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

        $page = "";

        switch ($_G["page"]) {
            case "add":
                $page = $this->renderShipmentEditAdd($render,'add');
                break;

            case "edit":
                $page = $this->renderShipmentEditAdd($render,'edit');
                break;
            
            default:
                $page = $this->renderShipmentList($render);
                break;
        }

        return $render->fetch("file:".$this->tplfolder."/".$page);
    }


    // ----------------------------------------------------------------------------------
    // --- [ Generate Customers List ] --------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function cb_rndCL($data)
    {
        $dt = array(
                array($data["shipment_id"], "align=\"center\""),
                array($data["shipment_title"], "align=\"left\""),
                array($data["shipment_price"], "align=\"left\""),
                array($data["active"], "align=\"center\"" ),
                array("<a href=\"?".$this->getURL()."&page=edit&id={$data[shipment_id]}\">edit</a>&nbsp;&nbsp;<a href=\"?".$this->getURL()."&action=doDeleteShipment&id={$data[shipment_id]}\">delete</a>","align=\"center\""),
                );
//      debug($dt, "CBfunction");
        return $dt;
    }

    function renderShipmentList(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        $pg = &$core->getSnippet("paginator");

        $params = array(
                    "cb_class" => &$this,
                    "cb_func" => cb_rndCL,
                    "urlbase" => $this->getURL(),
                    "caption" => "Packages shipping types list",
                    "emptymsg" => "No shipment methods collected yet",
                    "header" => array("ID", "Title", "Price", "Active", "Action"),
                    "items" => "",
                    "source" => "sql", // -- sql, array
                    "sql_items" => "select shipment_id, shipment_title , shipment_price, active from shipment",
                    "sql_count" => "select count(*) from shipment",
                    "ippg" => 15, // -- items per page
                    "nppv" => 5, // -- number pages per view
                    );

        $HTML = $pg->Render($params);
        $render->assign("shipment", $HTML);
            $render->assign('add_url',$this->getURL());

                #################
            #  UPS CP START #
            #################
        
        $sw = new myswitch('ups_tracker',$db);
        $ups_tracker_active = $sw->get_key_value();
        $render->assign('ups_track_status',$ups_tracker_active);
        
        $sw = new myswitch('ups_tit',$db);
        $ups_tit_active = $sw->get_key_value();
        $render->assign('ups_tit_status',$ups_tit_active);
        
        $sw = new myswitch('ups_av',$db);
        $ups_av_active = $sw->get_key_value();
        $render->assign('ups_av_status',$ups_av_active);
        
        $sw = new myswitch('ups_rate',$db);
        $ups_rate_active = $sw->get_key_value();
        $render->assign('ups_rate_status',$ups_rate_active);
                #################
            #  UPS CP END   #
            #################

        return "shipmentlist.tpl.php";
    }
    
    
    // ----------------------------------------------------------------------------------
    // --- [ Generate Customers Edit ] --------------------------------------------------
    // ----------------------------------------------------------------------------------

    function renderShipmentEditAdd(&$render,$event)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        
        $uid = $_GET["id"];
/*      if(empty($uid) || $uid<0)
        {
            header("location: ./?".$this->getURL());
            exit();
        }
*/       
        $render->assign("url", "?".$this->getURL());
        if($event != 'add'){
        $render->assign("id", $uid);
        $render->assign("usr", $db->getRow("select * from shipment where shipment_id={$uid}"));
                   }
        $render->assign("evenval", $event);


        return "shipmentedit.tpl.php";
    }
};


?>